package com.party.synthesis.impl;

import com.party.common.utils.StringUtils;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.EventGradation;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.TargetProject;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.distributor.DistributorRelationType;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.message.IdentityType;
import com.party.core.model.order.OrderForm;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ITargetProjectService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.order.IOrderFormService;
import com.party.notify.notifyPush.officialPartner.ICopartnerNotifySendService;
import com.party.notify.notifyPush.officialPartner.IOfficialPartnerNotifySendService;
import com.party.notify.notifyPush.servce.INotifySendService;
import com.party.synthesis.IProjectMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: party
 * @description: 众筹者消息业务接口实现
 * @author: Hua Qunhao
 * @create: 2019-03-29 13:49
 **/

@Service("ProjectMessageService")
public class ProjectMessageService implements IProjectMessageService {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private INotifySendService notifySendService;

    @Autowired
    private ITargetProjectService targetProjectService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IOfficialPartnerNotifySendService officialPartnerNotifySendService;

    @Autowired
    private ICopartnerNotifySendService copartnerNotifySendService;

    @Autowired
    private IOrderFormService orderFormService;

    @Autowired
    private IDistributorRelationService distributorRelationService;


    protected static Logger logger = LoggerFactory.getLogger(ProjectMessageService.class);


    /**
     * @Description: 众筹者报名__多场
     * @Param:
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019-03-29-0029
     */
    @Override
    public void gradationApplySend(String projectId, String memberId, String distributorId, Integer distributionType, EventGradation gradation) throws Exception {

        logger.info("众筹者报名__多场-------------------------开始");

        Project project = projectService.get(projectId);
        Member member = memberService.get(memberId);
        //根据选择替换金额和增加场次
        project = projectService.isGradationReplace(project, gradation);

        logger.info("众筹者报名---通知众筹者--------project--" + project + "------member-" + member);
        //发送众筹成功消息
        notifySendService.gradationSendLaunchProject(project, member);

        TargetProject targetProject = targetProjectService.findByProjectId(projectId);
        Activity activity = activityService.get(targetProject.getTargetId());

        try {
            // 合作商推送
            MemberGroup orgMember = memberGroupService.get(activity.getMember());
            officialPartnerNotifySendService.gradationSendCrowdfundApply(project, member, orgMember);
        } catch (Exception e) {
            logger.error("发起众筹——合作商离线推送异常", e);
        }

        if (StringUtils.isNotEmpty(distributorId) && null != distributionType) {
            try {
                // 联合发起人推送
                Member partnerReceiver = memberService.get(distributorId);
                if (partnerReceiver != null) {

                    logger.info("发起众筹——联合发起人消息"+"__________"+partnerReceiver);

                    if (DistributorRelationType.CROWDFUND_REPRESENT.getCode().equals(distributionType)) {
                        copartnerNotifySendService.gradationSendCrowdfundApply(project, member, partnerReceiver.getId(),
                                partnerReceiver.getMobile(), IdentityType.PARTNER.getCode());
                    }
                }
            } catch (Exception e) {
                logger.error("发起众筹——联合发起人消息推送异常", e);
            }
        }

        if (StringUtils.isNotEmpty(activity.getCosponsorId())) {
            try {
                // 合作渠道推送
                MemberGroup receiver = memberGroupService.get(activity.getCosponsorId());

                logger.info("发起众筹——合作渠道推送消息"+"__________"+receiver);
                if (receiver != null) {
                    copartnerNotifySendService.gradationSendCrowdfundApply(project, member, receiver.getId(),
                            receiver.getMobile(), IdentityType.COSPONSOR.getCode());
                }
            } catch (Exception e) {
                logger.error("发起众筹——合作渠道消息推送异常", e);
            }
        }
    }

    /**
     * @Description: 众筹者成功__多场
     * @Param:
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019-03-29-0029
     */
    @Override
    public void gradationCrowfundSuccessSend(String orderId, String projectId, EventGradation gradation) throws Exception {

        logger.info("众筹成功通知---多场-------------------------开始");
        OrderForm orderForm = orderFormService.get(orderId);

        Project project = projectService.get(projectId);
        Member member = memberService.get(orderForm.getMemberId());
        MemberGroup orgMember = memberGroupService.get(orderForm.getInitiatorId());

        //根据选择替换金额和增加场次
        project = projectService.isGradationReplace(project, gradation);


        logger.info("众筹成功通知---通知众筹者--------project--" + project + "------member-" + member + "---orderForm" + orderForm);

        // 发送推送短信

        try {
            notifySendService.gradationSendCorowdfund(orderForm, project, member);
        } catch (Exception e) {
            logger.error("众筹成功通知失败---通知众筹者", e);
        }
        try {
            officialPartnerNotifySendService.gradationSendCrowdfundSuccess(project, member, orgMember);
        } catch (Exception e) {
            logger.error("众筹成功通知失败---通知合作商", e);
        }

        try {
            // 代言人 合伙人 众筹成功消息推送
            DistributorRelation distributorRelation =
                    distributorRelationService.get(DistributorRelationType.CROWDFUND_REPRESENT.getCode(),
                            orderForm.getGoodsId(), null);
            if(null!=distributorRelation){
                String distributorId = distributorRelation.getDistributorId();
                if (StringUtils.isNotEmpty(distributorId)) {
                    // 联合发起人推送
                    Member partnerReceiver = memberService.get(distributorId);
                    if (partnerReceiver != null) {

                        logger.info("众筹成功通知---联合发起人推送--------partnerReceiver___"+partnerReceiver);
                        copartnerNotifySendService.gradationSendCrowdfundSuccess(project, member, partnerReceiver.getId(),
                                partnerReceiver.getMobile(), IdentityType.PARTNER.getCode());
                    }
                }
            }

        } catch (Exception e) {
            logger.error("众筹成功通知失败---通知联合发起人", e);
        }

        try {
            // 合作渠道推送
            Activity activity = activityService.get(orderForm.getGoodsId());
            if (StringUtils.isNotEmpty(activity.getCosponsorId())) {
                MemberGroup cosponsor = memberGroupService.get(activity.getCosponsorId());
                if (cosponsor != null) {

                    logger.info("众筹成功通知---合作渠道推送--------partnerReceiver___"+cosponsor);
                    copartnerNotifySendService.gradationSendCrowdfundSuccess(project, member, cosponsor.getId(),
                            cosponsor.getMobile(), IdentityType.COSPONSOR.getCode());
                }
            }
        } catch (Exception e) {
            logger.error("众筹成功通知失败---通知合作渠道", e);
        }

    }
}
